﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Win.Controls;
using PlanningGIS.Framework.Rule;

namespace PlanningGIS.Framework.Controls
{
    /// <summary>
    /// 选择规则
    /// </summary>
    public partial class FrmSelectRule : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmSelectRule()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="value"></param>
        public FrmSelectRule(zjgisRuleType value)
        {
            InitializeComponent();
            RuleType = value;
        }

        /// <summary>
        /// 规则类型
        /// </summary>
        public zjgisRuleType RuleType { private get; set; }

        /// <summary>
        /// 选择的规则
        /// </summary>
        public RuleXml Rule { get; private set; }

        /// <summary>
        /// 是否显示匹配方式
        /// </summary>
        public bool TypeComboxVisable
        {
            set { cboType.Visible = value; }
        }

        /// <summary>
        /// 匹配方式：0表示按图层名匹配；1表示按要素类名匹配
        /// </summary>
        public int MatchType
        {
            get { return cboType.SelectedIndex; }
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmSelectRule_Load(object sender, EventArgs e)
        {
            dgvRule.Rows.Clear();
            cboType.SelectedIndex = 0;           
            //switch (RuleType)
            //{
            //    case zjgisRuleType.dlgImpByCode:
            //        List<DataImportRule> dataImportRules = RuleConfig.rulesXmlContainer.BuildingRules;
            //        foreach (DataImportRule rule in dataImportRules)
            //        {
            //            int idx = dgvRule.Rows.Add();
            //            dgvRule.Rows[idx].Cells[0].Value = rule.RuleName;
            //            dgvRule.Rows[idx].Tag = rule;
            //        }
            //        break;
            //    case zjgisRuleType.dlgImpByLyr:
            //        List<DataMatchRule> dataMatchRules = RuleConfig.rulesXmlContainer.dataMatchtRules;
            //        foreach (DataMatchRule rule in dataMatchRules)
            //        {
            //            int idx = dgvRule.Rows.Add();
            //            dgvRule.Rows[idx].Cells[0].Value = rule.RuleName;
            //            dgvRule.Rows[idx].Tag = rule;
            //        }
            //        break;
            //    case zjgisRuleType.dlgExp:
            //        //List<DataExportRule> dataExportRules = RuleConfig.rulesXmlContainer.dataExportRules;
            //        //foreach (DataExportRule rule in dataExportRules)
            //        //{
            //        //    int idx = dgvRule.Rows.Add();
            //        //    dgvRule.Rows[idx].Cells[0].Value = rule.RuleName;
            //        //    dgvRule.Rows[idx].Tag = rule;
            //        //}
            //        break;
            //    case zjgisRuleType.dlgMerge:
            //        List<MergeRule> mergeRules = RuleConfig.rulesXmlContainer.mergeRules;
            //        foreach (MergeRule rule in mergeRules)
            //        {
            //            int idx = dgvRule.Rows.Add();
            //            dgvRule.Rows[idx].Cells[0].Value = rule.RuleName;
            //            dgvRule.Rows[idx].Tag = rule;
            //        }
            //        break;
            //    case zjgisRuleType.dlgCheck:
            //        //List<DataCheckRule> dataCheckRules = RuleConfig.rulesXmlContainer.dataCheckRules;
            //        //foreach (DataCheckRule rule in dataCheckRules)
            //        //{
            //        //    int idx = dgvRule.Rows.Add();
            //        //    dgvRule.Rows[idx].Cells[0].Value = rule.RuleName;
            //        //    dgvRule.Rows[idx].Tag = rule;
            //        //}
            //        break;
            //    case zjgisRuleType.pipCode:
            //        List<PipeLayerCodeRule> pipeLayerCodeRules = RuleConfig.rulesXmlContainer.pipeLayerRules;
            //        foreach (PipeLayerCodeRule rule in pipeLayerCodeRules)
            //        {
            //            int idx = dgvRule.Rows.Add();
            //            dgvRule.Rows[idx].Cells[0].Value = rule.RuleName;
            //            dgvRule.Rows[idx].Tag = rule;
            //        }
            //        break;
            //    case zjgisRuleType.pipImp:
            //        List<PipeImportRule> pipeImportRules = RuleConfig.rulesXmlContainer.pipeImportRules;
            //        foreach (PipeImportRule rule in pipeImportRules)
            //        {
            //            int idx = dgvRule.Rows.Add();
            //            dgvRule.Rows[idx].Cells[0].Value = rule.RuleName;
            //            dgvRule.Rows[idx].Tag = rule;
            //        }
            //        break;
            //    case zjgisRuleType.pnCheck:
            //        List<PNDataCheckRule> PNDataCheckRules = RuleConfig.rulesXmlContainer.pndataCheckRules;
            //        foreach (PNDataCheckRule rule in PNDataCheckRules)
            //        {
            //            int idx = dgvRule.Rows.Add();
            //            dgvRule.Rows[idx].Cells[0].Value = rule.RuleName;
            //            dgvRule.Rows[idx].Tag = rule;
            //        }
            //        break;
            //}
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItemCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItemOK_Click(object sender, EventArgs e)
        {
            if (dgvRule.SelectedRows.Count != 1)
            {
                MsgHelper.ShowInfomationMsgbox("请选择一个规则！");
                return;
            }
            DataGridViewRow dgvRow = dgvRule.SelectedRows[0];
            if (!(dgvRow.Tag is RuleXml))
            {
                MsgHelper.ShowInfomationMsgbox("选择了一个无效规则！");
                return;
            }
            Rule = dgvRow.Tag as RuleXml;
            DialogResult = DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// 双击选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvRule_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
            buttonItemOK_Click(null, null);
        }
    }
}
